package com.jingyanzi.resallocation.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.client.DicClientAPI;
import com.jingyanzi.resallocation.dao.ResSiteMapper;
import com.jingyanzi.resallocation.domain.DictionariesDO;
import com.jingyanzi.resallocation.domain.SiteDO;
import com.jingyanzi.resallocation.entity.ResSite;
import com.jingyanzi.resallocation.entity.ResSiteExample;
import com.jingyanzi.resallocation.entity.ResSiteExample.Criteria;
import com.jingyanzi.resallocation.query.SiteQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.ISiteService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("siteService")
public class SiteServiceImpl implements ISiteService {
	
	@Autowired
	private ResSiteMapper siteMapper;
	
	@Autowired
	private DicClientAPI dicClientAPI;

	@Override
	public ResultDO add(SiteDO domain) {
		
		ResSite entity = new ResSite();
		
		ResultDO result = new ResultSupport();
		
		if(!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			return result;
		}
		
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setModifiTime(entity.getCreateTime());
		entity.setIsDelete(1);
		
		int r = -1;
		
		try {
			r = siteMapper.insertSelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity.getId());
		return result;
	}

	@Override
	public ResultDO edit(SiteDO domain) {
		
		ResSite entity = new ResSite();
		
		ResultDO result = new ResultSupport();
		
		if(!BeanUtilsExtends.copyProperties(entity, domain)) {
			result.setSuccess(false);
			return result;
		}
		
		entity.setModifiTime(Calendar.getInstance().getTime());
		
		int r = -1;
		
		try {
			r = siteMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, entity.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer id) {
		
		ResSite entity = new ResSite();
		
		ResultDO result = new ResultSupport();
		
		entity.setId(id);
		entity.setIsDelete(-1);
		int r = -1;
		
		try {
			r = siteMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
			r = -1;
		}
		
		if(r < 0) {
			result.setSuccess(false);
			return result;
		}
		
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		
		ResSite entity = null;
		
		ResultDO result = new ResultSupport();
		
		try {
			entity = siteMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			return result;
		}
		
		if(entity == null) {
			result.setSuccess(false);
			return result;
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSiteDO(entity));
		return result;
	}

	@Override
	public ResultDO query(SiteQueryDO query) {
		
		ResultDO result = new ResultSupport();
		
		ResSiteExample example = new ResSiteExample();
		Criteria criteria = example.createCriteria();
		
		if(StringUtils.isNotEmpty(query.getSiteName())) {
			criteria.andSiteNameLike("%" + query.getSiteName() + "%");
		}
		
		if(query.getSceneType() != null && query.getSceneType() != 0) {
			criteria.andSceneTypeEqualTo(query.getSceneType());
		}
		if(query.getSceneClass() != null && query.getSceneClass() != 0) {
			criteria.andSceneClassEqualTo(query.getSceneClass());
		}
		if(StringUtils.isNoneEmpty(query.getRegion())) {
			criteria.andRegionLike("%" + query.getRegion() + "%");
		}
		
		List<ResSite> list = null;
		
		long count = 0;
		if(query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			count = siteMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		try {
			list = siteMapper.selectByExample(example);
		} catch(Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			return result;
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSiteDOList(list));
		return result;
	}
	
	private SiteDO getSiteDO(ResSite entity) {
		if (entity == null)
			return null;
		SiteDO dst = new SiteDO();
		dst = BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
		DictionariesDO dic = dicClientAPI.get(dst.getSceneClass());
		dst.setSceneClassName(dic.getDataItem());
		return dst;
	}
	
	private List<SiteDO> getSiteDOList(List<ResSite> list) {
		List<SiteDO> result = new ArrayList<SiteDO>();
		if (list != null && !list.isEmpty()) {
			for (ResSite entity : list) {
				SiteDO domain = getSiteDO(entity);
				if (domain != null) {
					result.add(domain);
				}
			}
		}
		return result;
	}

}
